package breeze.signal

import org.scalatest._
import org.scalatest.funsuite._
import breeze.linalg.{DenseVector, norm}

/**
 * Test for correctness of the haar transform
 */

class HaarTrTest extends AnyFunSuite {

  test("haarTr 1d of DenseVector[Double]") {
    assert(norm(haarTr(test16) - test16haarTransformed) < testNormThreshold)
  }
  test("iHaarTr 1d of DenseVector[Double]") {
    assert(norm(iHaarTr(test16haarTransformed) - test16) < testNormThreshold)
  }

  // Test Values
  val testNormThreshold = 1E-12

  val test16 = DenseVector[Double](0.814723686393179, 0.905791937075619, 0.126986816293506, 0.913375856139019,
    0.63235924622541, 0.0975404049994095, 0.278498218867048, 0.546881519204984, 0.957506835434298, 0.964888535199277,
    0.157613081677548, 0.970592781760616, 0.957166948242946, 0.485375648722841, 0.8002804688888, 0.141886338627215)

  /* [ 0.814723686393179, 0.905791937075619,  0.126986816293506, 0.913375856139019,
   *   0.63235924622541,  0.0975404049994095, 0.278498218867048, 0.546881519204984,
   *   0.957506835434298, 0.964888535199277,  0.157613081677548, 0.970592781760616,
   *   0.957166948242946, 0.485375648722841,  0.8002804688888,   0.141886338627215 ]
   * For every pair compute the sum and delta, the sum is the upper half, the delta the lower half,
   * normalize by 1/sqrt(2)
   * [ // sums
   *   [ 1.21658826449218773797, 0.73564750057039727347, 0.51611699296676579694, 0.58363160984471024047,
   *     1.35933880269662724608, 0.79776201661154974942, 1.02003165246496076300, 0.66621253860345485811],
   *   // deltas
   *   [ -0.06439497760834975834, -0.55606102272554036332, 0.37817402933723643789, -0.18977565162618037741,
   *     -0.00521964996049979503, -0.57486345889574299652, 0.33360682719547979029, 0.46555495420138585238 ]
   * ]
   * Repeat for the upper half
   * [ // sum of sums
   *   [ 1.38043914795066149999, 0.77763969464842574999, 1.52530061703586949999, 1.19235470224090099999 ],
   *   // delta of sums
   *   [ 0.34007647551813650000, -0.04774004342360625000, 0.39709475359770550000, 0.25018789472488599999 ]
   *   // deltas
   *   [ -0.06439497760834975834, -0.55606102272554036332, 0.37817402933723643789, -0.18977565162618037741,
   *     -0.00521964996049979503, -0.57486345889574299652, 0.33360682719547979029, 0.46555495420138585238 ]
   * ]
   * Repeat for sum of sums
   * [ // sum of sums of sums
   *   [ 1.52599218393703052442, 1.92167250518829630830 ],
   *   // delta of sums of sums
   *   [ 0.42624358112555448700, 0.23542831411988068719 ]
   *   // delta of sums, deltas
   *   [  0.34007647551813650000, -0.04774004342360625000, 0.39709475359770550000,  0.25018789472488599999,
   *     -0.06439497760834975834, -0.55606102272554036332, 0.37817402933723643789, -0.18977565162618037741,
   *     -0.00521964996049979503, -0.57486345889574299652, 0.33360682719547979029,  0.46555495420138585238 ]
   * ]
   * Final repeat for the sum of sums of sums -:-)
   * [  2.43786708093792887498, -0.27978823833884162499, 0.42624358112555448700,  0.23542831411988068719,
   *    0.34007647551813650000, -0.04774004342360625000, 0.39709475359770550000,  0.25018789472488599999,
   *   -0.06439497760834975834, -0.55606102272554036332, 0.37817402933723643789, -0.18977565162618037741,
   *   -0.00521964996049979503, -0.57486345889574299652, 0.33360682719547979029,  0.46555495420138585238 ]
   */
  val test16haarTransformed = DenseVector[Double](
    2.43786708093792887498, -0.27978823833884162499, 0.42624358112555448700, 0.23542831411988068719,
    0.34007647551813650000, -0.04774004342360625000, 0.39709475359770550000, 0.25018789472488599999,
    -0.06439497760834975834, -0.55606102272554036332, 0.37817402933723643789, -0.18977565162618037741,
    -0.00521964996049979503, -0.57486345889574299652, 0.33360682719547979029, 0.46555495420138585238
  )

}
